package com.jfinal.demo.plugins;

import com.jfinal.kit.PathKit;

import java.io.*;
import java.util.Properties;

/**
 * Title:
 * Description:
 *
 * @version 2.0
 * @author: Yung Fu(FUYUNGCN AT GMAIL DOT com)
 * @since: 14-10-30 下午11:33
 */
public class PropertiesUtil {

    public static Properties loadPropertyFile(String config) {
        InputStream inputStream = null;
        Properties p = null;
        String fullFile;
        if (config.startsWith(File.separator)) {
            fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF" + config;
        } else {
            fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + config;
        }

        try {
            inputStream = new FileInputStream(new File(fullFile));
            p = new Properties();
            p.load(inputStream);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("Properties file not found: " + fullFile);
        } catch (IOException e) {
            throw new IllegalArgumentException("Properties file can not be loading: " + fullFile);
        } finally {
            try {
                if (inputStream != null) inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (p == null)
            throw new RuntimeException("Properties file loading failed: " + fullFile);
        return p;
    }

}
